package com.example.contactlist;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class Input extends Activity {
	private final int REQUEST_CODE_NAME = 1;
	private final int REQUEST_CODE_KANA = 2;
	private final int REQUEST_CODE_ADR = 3;
	private final int REQUEST_CODE_TEL = 4;
	private final int REQUEST_CODE_MAIL = 5;
	private final int REQUEST_CODE_CHECK = 6;
	TextView editName = null;
	TextView editKana = null;
	TextView editAdr = null;
	TextView editTel = null;
	TextView editMail = null;
	Contact contact = null;
	ListAdapter ada = null;
	Intent i = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_input);

		editName = (TextView)findViewById(R.id.editName);
		editKana = (TextView)findViewById(R.id.editKana);
		editAdr = (TextView)findViewById(R.id.editAdr);
		editTel = (TextView)findViewById(R.id.editTel);
		editMail = (TextView)findViewById(R.id.editMail);
		
//		i = this.getIntent();
//		contact = (Contact)i.getSerializableExtra("contact");
//		ada = (ListAdapter)i.getSerializableExtra("Adapter");
//		if(contact != null) {
//			editName.setText(contact.getName());
//			editKana.setText(contact.getKana());
//			editAdr.setText(contact.getAdr());
//			editTel.setText(contact.getTel());
//			editMail.setText(contact.getMail());
//			
//		}
		contact = new Contact();
	}
	
	public void cancel_onClick(View view) {
		setResult(RESULT_OK, null);
		finish();
	}
	
	public void entry_onClick(View view) {
		if(contact != null) {
			i = new Intent(this, com.example.contactlist.Check.class);
			if(contact.inputCheck()) {
				//確認画面へ飛ぶ
				i.putExtra("contact", contact);
				startActivityForResult(i, REQUEST_CODE_CHECK);
			}
			else {
				Log.i("input", "error");
				//エラー表示
//				TextView txtError = (TextView)findViewById(R.id.txtError);
//				txtError.setText(contact.getAlertName());
				//ダイアログを表示
				StringBuffer sb = new StringBuffer();
				if(contact.getAlertName() != null) {
					sb.append(contact.getAlertName() + "\n");
				}
				if(contact.getAlertTel() != null) {
					sb.append(contact.getAlertTel() + "\n");
				}
				if(contact.getAlertMail() != null) {
					sb.append(contact.getAlertMail());
				}
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("エラーメッセージ")
				.setMessage(sb.toString())
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(contact.getAlertMail() != null) {
							mail_onClick(null);
						}
						if(contact.getAlertTel() != null) {
							tel_onClick(null);
						}						
						if(contact.getAlertName() != null) {
							name_onClick(null);
						}
					}
				})
				.show();
			}
		}
	}
	
	public void name_onClick(View view) {
		i = new Intent(this, com.example.contactlist.NameActivity.class);
		if (contact != null) {
			i.putExtra("param", contact.getName());
			i.putExtra("contact", contact);
		}
		this.startActivityForResult(i, REQUEST_CODE_NAME);
	}
	
	public void kana_onClick(View view) {
		i = new Intent(this, com.example.contactlist.KanaActivity.class);
		if(contact != null) {
			i.putExtra("param", contact.getKana());
		}
		this.startActivityForResult(i, REQUEST_CODE_KANA);
	}
	
	public void adr_onClick(View view) {
		i = new Intent(this, com.example.contactlist.AdrActivity.class);
		if(contact != null) {
			i.putExtra("param", contact.getAdr());
		}
		this.startActivityForResult(i, REQUEST_CODE_ADR);
	}
	
	public void tel_onClick(View view) {
		i = new Intent(this, com.example.contactlist.TelActivity.class);
		if(contact != null) {
			i.putExtra("param", contact.getTel());
			i.putExtra("contact", contact);
		}
		this.startActivityForResult(i, REQUEST_CODE_TEL);
	}
	
	public void mail_onClick(View view) {
		i = new Intent(this, com.example.contactlist.MailActivity.class);
		if(contact != null) {
			i.putExtra("param", contact.getMail());
			i.putExtra("contact", contact);
		}
		this.startActivityForResult(i, REQUEST_CODE_MAIL);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK) {
			if(contact == null) {
				contact = new Contact();
			}
			TextView editText = null;
			String param = data.getStringExtra("param");
			if(requestCode == REQUEST_CODE_NAME) {
				contact.setName(param);
				editText = (TextView)findViewById(R.id.editName);
			}
			else if(requestCode == REQUEST_CODE_KANA) {
				contact.setKana(param);
				editText = (TextView)findViewById(R.id.editKana);
			}
			else if(requestCode == REQUEST_CODE_ADR) {
				contact.setAdr(param);
				editText = (TextView)findViewById(R.id.editAdr);
			}
			else if(requestCode == REQUEST_CODE_TEL) {
				contact.setTel(param);
				editText = (TextView)findViewById(R.id.editTel);
			}
			else if(requestCode == REQUEST_CODE_MAIL) {
				contact.setMail(param);
				editText = (TextView)findViewById(R.id.editMail);
			}
			else if(requestCode == REQUEST_CODE_CHECK) {
				setResult(RESULT_OK, data);
				finish();
			}
			if(editText != null) {
				editText.setText(param);				
			}
			
		}
	}

}
